package com.boc.langxiang.ui.adapter.adm;

import android.content.Context;
import android.widget.ImageView;

import com.boc.langxiang.R;
import com.boc.langxiang.entity.response.PhotoEntity;
import com.boc.langxiang.entity.response.ShaloListEntity;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.boc.langxiang.util.L;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.TimeUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * CreateTime: 2016-06-17上午9:23
 * Author: wjkjinke00@126.com
 * Description:
 */
public class ShaloListAdapter extends BaseQuickAdapter<ShaloListEntity> {


    public ShaloListAdapter(Context context, List<ShaloListEntity> data) {
        super( R.layout.item_list_shalo, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ShaloListEntity item) {
        helper.setTag(R.id.item, item);

        int layoutPosition = helper.getLayoutPosition();
        int prePosition = layoutPosition - 1;
        int nextPosition = layoutPosition + 1;

        long CurrentTimeline = item.getTimeline();

        if (mData.size() == 1) {
            setVisible(helper, true, true, false, true, false);
        } else if (mData.size() > 1) {
            if (layoutPosition == 0) {
                ShaloListEntity nextEntity = mData.get(nextPosition);
                long nextTimeline = nextEntity.getTimeline();
                boolean nextSameDay = TimeUtil.isSameDay(CurrentTimeline, nextTimeline);

                if (nextSameDay) {
                    setVisible(helper, true, true, false, false, false);
                } else {
                    setVisible(helper, true, true, false, true, false);
                }

            } else {
                ShaloListEntity preEntity = mData.get(prePosition);
                long preTimeline = preEntity.getTimeline();
                boolean preSameDay = TimeUtil.isSameDay(CurrentTimeline, preTimeline);

                if (layoutPosition < mData.size() - 1) {
                    ShaloListEntity nextEntity = mData.get(nextPosition);
                    long nextTimeline = nextEntity.getTimeline();
                    boolean nextSameDay = TimeUtil.isSameDay(CurrentTimeline, nextTimeline);

                    if (preSameDay && nextSameDay) {
                        setVisible(helper, false, false, true, false, false);
                    } else if (preSameDay && !nextSameDay) {
                        setVisible(helper, false, false, false, false, true);
                    } else if (!preSameDay && nextSameDay) {
                        setVisible(helper, true, true, false, false, false);
                    } else if (!preSameDay && !nextSameDay) {
                        setVisible(helper, true, true, false, true, false);
                    }

                } else if (layoutPosition == mData.size() - 1) {
                    if (preSameDay) {
                        setVisible(helper, false, false, false, false, true);
                    } else {
                        setVisible(helper, true, true, false, true, false);
                    }
                }
            }

        }

        String s = TimeUtil.formatTime(CurrentTimeline, TimeUtil.TYPE_Y_M_D);
        String[] split = s.split("-");
        helper.setText(R.id.day, split[2])
                .setText(R.id.year, split[0] + "." + split[1])
                .setText(R.id.title, StringUtil.getValue(item.getTitle()))
                .setText(R.id.want, "想参与 " + StringUtil.getValue(item.getCollection() + ""))
                .setText(R.id.join, "参与 " + StringUtil.getValue(item.getOrder_num() + ""));
        List<PhotoEntity> photo = item.getPhoto();
        if (photo != null && !photo.isEmpty()) {
            PhotoEntity photoEntity = photo.get(0);
            if (photoEntity != null) {

                PicassoImageLoader.getInstance().displayUrlImage(mContext, photoEntity.getUrl()
                        , (ImageView) helper.getView(R.id.image));
            } else {
                PicassoImageLoader.getInstance().displayUrlImage(mContext, null
                        , (ImageView) helper.getView(R.id.image));
            }
        } else {
            PicassoImageLoader.getInstance().displayUrlImage(mContext, null
                    , (ImageView) helper.getView(R.id.image));
        }


    }


    private void setVisible(BaseViewHolder helper, boolean time, boolean line1, boolean line2, boolean line3, boolean line4) {
        helper.setVisible(R.id.time_gp, time)
                .setVisible(R.id.line_01, line1)
                .setVisible(R.id.line_02, line2)
                .setVisible(R.id.line_03, line3)
                .setVisible(R.id.line_04, line4);
    }

}
